<script setup lang="ts" name="EaTaskAppeal">
import { Plus, Search, ArrowDown } from '@element-plus/icons-vue'
import type { EaTaskAppeal } from './type'
import Detail from '@/views/ea/ea-task-appeal/Detail.vue'

const baseApi = '/ea/ea-task-appeal'

const { queryParam, loading, dataList, pagination, getList, onSearch, resetFilter, remove, batchRemove } =
  useList<EaTaskAppeal>({ baseApi, initQueryParam: {} })

defineExpose({
  refresh: onSearch,
  addCondition: (key: keyof EaTaskAppeal, value: any, refresh = false) => {
    queryParam[key] = value
    if (refresh) onSearch()
  }
})

const sortChange = ({ column, prop, order }: { column: { sortBy?: string }; prop: string; order: string }) => {
  let orderBy: 'ASC' | 'DESC' | undefined
  switch (order) {
    case 'ascending':
      orderBy = 'ASC'
      break
    case 'descending':
      orderBy = 'DESC'
      break
    default:
      orderBy = undefined
  }
  pagination.orderBy = orderBy ? `${column.sortBy ?? prop}:${orderBy}` : undefined
  onSearch()
}

const detailRef = ref()
const detailVisible = ref(false)

const openDetail = (id: string) => {
  detailVisible.value = true

  nextTick(() => detailRef.value?.init(id))
}

const closeDetail = () => {
  detailVisible.value = false
}
const refuse = (id: string) => {
  ElMessageBox.prompt('请输入拒绝原因', '提示', {
    inputValidator: value => {
      if (!value) {
        return '请输入拒绝原因'
      }
      return true
    }
  }).then(({ value }) => {
    api
      .post(`${baseApi}/appealApprove`, {
        id,
        appealStatus: 'FAIL',
        handleRemark: value
      })
      .then(() => {
        getList()
        ElMessage({
          type: 'success',
          message: `拒绝原因:${value}`
        })
      })
  })
}
const pass = (id: string) => {
  ElMessageBox.confirm('确定通过吗？', '提示', {
    type: 'warning'
  }).then(() => {
    api
      .post(`${baseApi}/appealApprove`, {
        id,
        appealStatus: 'SUCCESS'
      })
      .then(() => {
        getList()
        ElMessage({
          type: 'success',
          message: `通过`
        })
      })
  })
}
const handleOperation = (code: string, value?: string | string[]) => {
  switch (code) {
    case 'refuse':
      refuse(value as string)
      break
    case 'pass':
      pass(value as string)
      break
    case 'detail':
      openDetail(value as string)
      break
    case 'remove':
      remove(value as string)
      break
    case 'batchRemove':
      batchRemove(value as string[])
      break
    default:
      throw new Error(`不存在的操作编码${code}!`)
  }
}

const refreshData = (haveNewData?: boolean) => {
  haveNewData ? onSearch() : getList()
}

const router = useRouter()

const activated = () => {
  nextTick(() => {
    const query = router.currentRoute.value.query
    for (const queryKey in query) {
      queryParam[queryKey as keyof EaTaskAppeal] = query[queryKey] as any
    }

    onSearch()
  })
}

router.currentRoute.value.meta.keepAlive ? onActivated(activated) : activated()
</script>

<template>
  <div class="list-page">
    <el-space wrap class="list-operation">
      <el-space>
        <span class="search">
          <el-input v-model="queryParam.appealReason" placeholder="申诉理由" clearable @change="onSearch" />
        </span>
        <el-button :icon="Search" type="primary" @click="onSearch">查询</el-button>
        <el-button title="重置查询条件" @click="resetFilter">重置</el-button>
      </el-space>
    </el-space>

    <el-table
      ref="tableRef"
      v-loading="loading"
      class="list-body"
      :data="dataList"
      height="100%"
      stripe
      row-key="id"
      style="border-top: 1px solid var(--el-border-color-lighter)"
      @row-dblclick="(row: EaTaskAppeal) => checkPermission('detail') && handleOperation('detail', row.id)"
      @sort-change="sortChange"
    >
      <el-table-column label="申诉理由" prop="appealReason" show-overflow-tooltip />
      <el-table-column label="申诉状态" prop="appealStatusLabel" show-overflow-tooltip>
        <template #default="{ row }: { row: EaTaskAppeal }">
          <el-tag
            v-if="row.appealStatusLabel"
            effect="dark"
            type="info"
            :color="(row.appealStatusLabel as LabelValue<{ color?: string }>).ext?.color"
          >
            {{ (row.appealStatusLabel as LabelValue).label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申诉理由类型" prop="appealType" show-overflow-tooltip />
      <el-table-column label="申诉发起端" prop="userTypeLabel" show-overflow-tooltip>
        <template #default="{ row }: { row: EaTaskAppeal }">
          <el-tag
            v-if="row.userTypeLabel"
            effect="dark"
            type="info"
            :color="(row.userTypeLabel as LabelValue<{ color?: string }>).ext?.color"
          >
            {{ (row.userTypeLabel as LabelValue).label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申诉人" prop="appealUserId" show-overflow-tooltip />
      <el-table-column label="被申诉人" prop="beAppealUserId" show-overflow-tooltip />
      <el-table-column label="处理人" prop="handleUserId" show-overflow-tooltip />
      <el-table-column label="处理时间" prop="handleTime" show-overflow-tooltip />
      <el-table-column label="处理备注" prop="handleRemark" show-overflow-tooltip />
      <el-table-column label="任务Id" prop="taskId" show-overflow-tooltip />
      <el-table-column label="任务记录Id" prop="taskRecordId" show-overflow-tooltip />
      <el-table-column label="操作" fixed="right" :width="200">
        <template #default="{ row }: { row: EaTaskAppeal }">
          <el-space>
            <el-button
              v-if="row.appealStatus === 'APPEAL'"
              type="danger"
              text
              bg
              size="small"
              @click="handleOperation('refuse', row.id)"
            >
              拒绝
            </el-button>
            <el-button
              v-if="row.appealStatus === 'APPEAL'"
              type="primary"
              text
              bg
              size="small"
              @click="handleOperation('pass', row.id)"
            >
              通过
            </el-button>
            <el-button
              v-has-permission="'detail'"
              type="primary"
              text
              bg
              size="small"
              @click="handleOperation('detail', row.id)"
            >
              详情
            </el-button>
          </el-space>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="pagination.total"
      v-model:current-page="pagination.current"
      v-model:page-size="pagination.pageSize"
      :page-sizes="[10, 15, 20, 30, 50, 100]"
      small
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      @size-change="getList()"
      @current-change="getList()"
    />

    <el-dialog v-model="detailVisible" width="60%" title="详情" draggable @close="closeDetail">
      <Detail ref="detailRef" />

      <template #footer>
        <el-button @click="closeDetail">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss"></style>
